﻿@model RH.Web.ViewModels.AddressViewModel
@using RH.Domain;
@using RH.Common.Extensions;
@using RH.Web.Utility;
@using RH.Common.Extensions;
@{  
    if (Model == null)
    {
        throw new ArgumentException("The Model Address cannot be null.");
    }
    RH.Domain.AppEntities context = new RH.Domain.AppEntities();
    if (Model.LocationID > 0)
    {
        //Model.DistrictID = Model.DistrictID;

        RH.Domain.Location location = context.Locations.Include("District").FirstOrDefault(C => C.ID == Model.LocationID);
        if (location != null)
        {
            Model.DistrictID = location.DistrictID;
            Model.CountryID = location.District.CountryID;
        }
    }
    if (Model.ZipCode.IsUnknown())
    {
        Model.ZipCode = String.Empty;
    }
    String _FieldPrefix = ViewData.TemplateInfo.HtmlFieldPrefix;
    if (Model.CountryID <= 0 && ConfigReader.ForceCountryID > 0)
    {
        Model.CountryID = ConfigReader.ForceCountryID;
    }

    //Here we found a nasty bug in ASP .NET MVC3, so had to do following ugly work-around.
    //In the next release or ASP.NET MVC this bug should be resolved, then this code should be rewritten.
    http://connect.microsoft.com/VisualStudio/feedback/details/654543/asp-net-mvc-possible-mvc-bug-when-working-with-editortemplates-and-drop-down-lists#
    var _Countries = context.Countries.OrderBy(P => P.Name).ToList();    
    var _Districts = (from P in context.Districts where P.CountryID == Model.CountryID orderby P.Name select P).ToList();
    var _Locations = (from P in context.Locations where P.DistrictID == Model.DistrictID orderby P.Name select P).ToList();
    var _AddressTypes = from RH.Web.ViewModels.AddressType e in Enum.GetValues(typeof(RH.Web.ViewModels.AddressType))
                        select new { ID = (int)e, Name = e.ToString() };    
}

    @Html.HiddenFor(model => model.ID)
    <div class="editor-label">
        @Html.LabelFor(model => model.StreetAddress)
    </div>
    <div class="editor-field">
        @if (Model.IsMinimalView) {
            @Html.TextBoxFor(model => model.StreetAddress, new { @class = "rh-long-text"})
        }
        else {
            @Html.TextBoxFor(model => model.StreetAddress)
        }
        
        @Html.ValidationMessageFor(model => model.StreetAddress)
    </div>
@if (!Model.IsMinimalView) { 
    <div class="editor-label">
        @Html.LabelFor(model => model.Email)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.Email)
        @Html.ValidationMessageFor(model => model.Email)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.EmailOffice)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.EmailOffice)
        @Html.ValidationMessageFor(model => model.EmailOffice)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.Phone)
        <div class="rh-label-hint">If you want to give a foreign (Non Bangladeshi) number, please add it with the country code.<br/>Example: A Swedish number woulbe be +46000000000</div>
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.Phone)
        @Html.ValidationMessageFor(model => model.Phone)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.Fax)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.Fax)
        @Html.ValidationMessageFor(model => model.Fax)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.ZipCode)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.ZipCode)
        @Html.ValidationMessageFor(model => model.ZipCode)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.WebSite)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.WebSite)
        @Html.ValidationMessageFor(model => model.WebSite)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.AddressType)
    </div>
    <div class="editor-field">
        @*We forced to bind dropdownlists as follows because of the following bug in ASP .NET MVC Model Binder
            http://connect.microsoft.com/VisualStudio/feedback/details/654543/asp-net-mvc-possible-mvc-bug-when-working-with-editortemplates-and-drop-down-lists#*@
        @Html.DropDownListFor(
                model => model.AddressType,
                new SelectList(_AddressTypes, "ID", "Name", Model.AddressType)
                , "Please Select")
        @Html.ValidationMessageFor(model => model.AddressType)
    </div>
}
<div class="editor-label@{if(ConfigReader.ForceCountryID > 0 ){<text> hidden</text>}}">
    @Html.LabelFor(model => model.CountryID)
</div>
<div class="editor-field@{if(ConfigReader.ForceCountryID > 0 ){<text> hidden</text>}}">
    @Html.DropDownListFor(
            model => model.CountryID,
            new SelectList(_Countries, "ID", "Name", Model.CountryID)
            , "Please Select")
    @Html.ValidationMessageFor(model => model.CountryID)
</div>
<div class="editor-label">
    @Html.LabelFor(model => model.DistrictID)
</div>
<div class="editor-field">
    @Html.DropDownListFor(
            model => model.DistrictID,
            new SelectList(_Districts, "ID", "Name", Model.DistrictID)
            , "Please Select")
    @Html.ValidationMessageFor(model => model.DistrictID)
</div>
<div class="editor-label">
    @Html.LabelFor(model => model.LocationID)
</div>
<div class="editor-field">
    @Html.DropDownListFor(
            model => model.LocationID,
            new SelectList(_Locations, "ID", "Name", Model.LocationID)
            , "Please Select")
    @Html.ValidationMessageFor(model => model.LocationID)
</div>
@*	@Html.HiddenFor(model => model.ModifiedBy)
    @Html.HiddenFor(model => model.CreatedOn)
    @Html.HiddenFor(model => model.ModifiedOn)*@
@Html.HiddenFor(model => model.IsMinimalView)
<div id="@AppConstants.UI.DIV_ADDRESS_FIELD_PREFIX" class="hidden">@_FieldPrefix</div>


<script language="javascript" type="text/javascript">
    var _Country = $('#@(_FieldPrefix)_CountryID');
    var _District = $('#@(_FieldPrefix)_DistrictID');
    var _Location = $('#@(_FieldPrefix)_LocationID');

    $(document).ready(function () {
        $(_Country).change(function () {
            Ajax('@Url.PublicUrl("GetDistrictsByCountry", "Ajax")'
            , 'countryID=' + $(this).val()
            , BindDistricts_Success);
            $(_Location).find('option:gt(0)').remove();
        });
        $(_District).change(function () {
            Ajax('@Url.PublicUrl("GetLocationsByDistrict", "Ajax")'
            , 'districtID=' + $(this).val()
            , BindLocations_Success);
        });
        //if ($(_Country).val() > 0) $(_Country).trigger('change');
    });
    function BindDistricts_Success(result) {
        var types = eval(result);
        BindDDL(types, _District);
    }
    function BindLocations_Success(result) {
        var types = eval(result);
        BindDDL(types, _Location);
    }
</script>

